package com.quick.android.handler.http.request;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

/**
 * 文件下载请求
 * 
 * @author quick
 * 
 */
public class DownloadRequest extends Request<byte[]> {

	private final Listener<byte[]> mListener;
	
	private String path;
	
	/**
	 * Creates a new GET request.
	 * 
	 * @param url
	 *            URL to fetch the string at
	 * @param listener
	 *            Listener to receive the byte array response
	 */
	public DownloadRequest(String url,String path, Listener<byte[]> listener,
			Response.ErrorListener errorListener) {
		this(Method.GET, url,path, listener, errorListener);
	}

	/**
	 * Creates a new request with the given method.
	 * 
	 * @param method
	 *            the request {@link Method} to use
	 * @param url
	 *            URL to fetch the byte array at
	 * @param listener
	 *            Listener to receive the byte array response or error message
	 */
	public DownloadRequest(int method, String url,String path, Listener<byte[]> listener,
			Response.ErrorListener errorListener) {
		super(method, url, errorListener);
		mListener = listener;
		this.path = path;
	}

	@Override
	protected void deliverResponse(byte[] response) {
		if (null != mListener) {
			mListener.onResponse(response);
		}
	}

	@Override
	protected Response<byte[]> parseNetworkResponse(NetworkResponse response) {
		return Response.success(response.data,
				HttpHeaderParser.parseCacheHeaders(response));
	}

	@Override
	public String getBodyContentType() {
		return "application/octet-stream";
	}
	
	
	
}
